package com.basics.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.basics.api.request.channel.*;
import com.basics.api.response.channel.ChannelPostListData;
import com.basics.api.response.channel.CommentData;
import com.basics.api.response.channel.PostCommentListData;
import com.basics.api.response.channel.PostInfoData;
import com.basics.api.service.PostApiService;
import com.basics.channel.entity.CyChannelTopic;
import com.basics.framework.web.domain.RestResult;
import com.basics.post.entity.CyChannelPost;
import com.basics.post.service.CyChannelPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/post/")
@Api(tags = "帖子相关")
public class PostApiController extends BaseApiController{

    @Autowired
    private CyChannelPostService cyChannelPostService;
    @Autowired
    private PostApiService postApiService;


    @GetMapping("/selectTopicListForPost")
    @ApiOperation("频道详情-获取频道下的话题板块列表 用于帖子导航")
    public RestResult<List<CyChannelTopic>> selectTopicListForPost(@RequestParam(value = "channelId")Long channelId){
        return RestResult.success(postApiService.selectTopicListForPost(channelId,getCurrentUser().getUserId()));
    }


    @GetMapping("selectChannelPostList")
    @ApiOperation(value = "频道详情-获取频道帖子列表")
    public RestResult<IPage<ChannelPostListData>> selectChannelPostList(ChannelPostReq req){
        req.setOrganizationId(getOrganizationId(true));
        return RestResult.success(postApiService.selectChannelPostList(req,getCurrentUser().getUserId()));
    }


    @GetMapping("getPostInfo")
    @ApiOperation(value = "帖子详情-获取帖子详情")
    public RestResult<PostInfoData> getPostInfo(@RequestParam("postId")Long postId){
        return RestResult.success(postApiService.getPostInfo(postId,getCurrentUser().getUserId()));
    }

    @GetMapping("selectCommentList")
    @ApiOperation(value = "帖子详情-获取帖子评论列表")
    public RestResult<IPage<PostCommentListData>> selectCommentList(CommentReq req){
        return RestResult.success(postApiService.selectCommentList(req,getCurrentUser().getUserId()));
    }

    @GetMapping("selectSubCommentList")
    @ApiOperation(value = "帖子详情-获取帖子子评论回复列表")
    public RestResult<IPage<PostCommentListData>> selectSubCommentList(SubCommentReq req){
        return RestResult.success(postApiService.selectSubCommentList(req,getCurrentUser().getUserId()));
    }

    @PostMapping("pubPost")
    @ApiOperation(value = "发布-发布帖子")
    public RestResult<String> pubPost(@RequestBody PubPostReq req){
        postApiService.pubPost(req,getCurrentUser().getUserId(),getOrganizationId());
        return RestResult.success();
    }

    @GetMapping("deletePost")
    @ApiOperation(value = "帖子详情-删除帖子(右上角三个点 点开有删除跟编辑)")
    public RestResult<String> deletePost(@RequestParam(value = "postId")Long postId){
        postApiService.deletePost(postId,getCurrentUser().getUserId());
        return RestResult.success();
    }


    @PostMapping("pubComment")
    @ApiOperation(value = "帖子详情-发布评论")
    public RestResult<CommentData> pubComment(@RequestBody PubCommentReq req){
        return RestResult.success(postApiService.pubComment(req,getCurrentUser().getUserId()));
    }

    @PostMapping("priseComment")
    @ApiOperation(value = "帖子详情-点赞评论")
    public RestResult<String> priseComment(@RequestParam("commentId")String commentId){
        postApiService.priseComment(commentId,getCurrentUser().getUserId());
        return RestResult.success();
    }

    @PostMapping("prisePost")
    @ApiOperation(value = "帖子详情/频道详情/点击通知或者右上角邮件图标进入-点赞帖子")
    public RestResult<String> prisePost(@RequestParam("postId")Long postId){
        postApiService.prisePost(postId,getCurrentUser().getUserId());
        return RestResult.success();
    }


    @GetMapping("getPostInfoForEdit")
    @ApiOperation(value = "发布-获取帖子内容用于编辑")
    public RestResult<CyChannelPost> getPostInfoForEdit(@RequestParam("postId")Long postId){
        CyChannelPost post = cyChannelPostService.getById(postId);
        if(!post.getUserId().equals(getCurrentUser().getUserId())){
            return RestResult.error("无权限编辑");
        }
        return RestResult.success(post);
    }

    @ApiOperation(value = "帖子详情-删除评论")
    @GetMapping("deleteComment")
    public RestResult<String> deleteComment(@RequestParam("commentId")String commentId){
        postApiService.deleteComment(commentId,getOrganizationId(),getCurrentUser().getUserId());
        return RestResult.success();
    }

}
